Source for file Copy of ez_sql.php

Documentation is available at Copy of ez_sql.php

  1. <?php
  2.  
  3.  
  4.  
  5. // ==================================================================
  6. // Author: Justin Vincent (justin@visunet.ie)
  7.  
  8. // Web: http://php.justinvincent.com
  9.  
  10. // Name: ezSQL
  11.  
  12. // Desc: Class to make it very easy to deal with mySQL database connections.
  13.  
  14. //
  15.  
  16. // !! IMPORTANT !!
  17.  
  18. //
  19.  
  20. // Please send me a mail telling me what you think of ezSQL
  21.  
  22. // and what your using it for!! Cheers. [ justin@visunet.ie ]
  23.  
  24. //
  25.  
  26. // ==================================================================
  27.  
  28. // User Settings -- CHANGE HERE
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. define("EZSQL_DB_USER", "testing"); // <-- mysql db user
  36.  
  37. define("EZSQL_DB_PASSWORD", "testing"); // <-- mysql db password
  38.  
  39. define("EZSQL_DB_NAME", "piCMS"); // <-- mysql db pname
  40.  
  41. define("EZSQL_DB_HOST", "localhost:3306"); // <-- mysql server host
  42. // ==================================================================
  43.  
  44. // ezSQL Constants
  45.  
  46.  
  47. define("EZSQL_VERSION","1.26");
  48.  
  49. define("OBJECT","OBJECT",true);
  50.  
  51. define("ARRAY_A","ARRAY_A",true);
  52.  
  53. define("ARRAY_N","ARRAY_N",true);
  54.  
  55.  
  56.  
  57.  
  58.  
  59. // ==================================================================
  60. // The Main Class
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. class db {
  68.  
  69.  
  70.  
  71. var $trace = false; // same as $debug_all
  72.  
  73.  
  74. var $debug_all = false; // same as $trace
  75.  
  76.  
  77. var $show_errors = true;
  78.  
  79. var $num_queries = 0;
  80.  
  81. var $last_query;
  82.  
  83. var $col_info;
  84.  
  85. var $debug_called;
  86.  
  87. var $vardump_called;
  88.  
  89.  
  90.  
  91. // ==================================================================
  92.  
  93. // DB Constructor - connects to the server and selects a database
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. function db($dbuser, $dbpassword, $dbname, $dbhost)
  101.  
  102. {
  103.  
  104.  
  105.  
  106. $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
  107.  
  108.  
  109.  
  110. if ( ! $this->dbh )
  111.  
  112. {
  113.  
  114. $this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?</ol>");
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. $this->select($dbname);
  123.  
  124.  
  125.  
  126. }
  127.  
  128.  
  129.  
  130. // ==================================================================
  131.  
  132. // Select a DB (if another one needs to be selected)
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. function select($db)
  140.  
  141. {
  142.  
  143. if ( !@mysql_select_db($db,$this->dbh))
  144.  
  145. {
  146.  
  147. $this->print_error("<ol><b>Error selecting database <u>$db</u>!</b><li>Are you sure it exists?<li>Are you sure there is a valid database connection?</ol>");
  148.  
  149. }
  150.  
  151. }
  152.  
  153.  
  154.  
  155. // ====================================================================
  156.  
  157. // Format a string correctly for safe insert under all PHP conditions
  158.  
  159.  
  160.  
  161.  
  162. function escape($str)
  163.  
  164. {
  165.  
  166. return mysql_escape_string(stripslashes($str));
  167.  
  168. }
  169.  
  170.  
  171.  
  172. // ==================================================================
  173.  
  174. // Print SQL/DB error.
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. function print_error($str = "")
  182.  
  183. {
  184.  
  185.  
  186. // All erros go to the global error array $EZSQL_ERROR..
  187.  
  188. global $EZSQL_ERROR;
  189.  
  190.  
  191.  
  192. // If no special error string then use mysql default..
  193.  
  194. if ( !$str )
  195.  
  196. {
  197.  
  198. $str = mysql_error($this->dbh);
  199.  
  200. $error_no = mysql_errno($this->dbh);
  201.  
  202. }
  203.  
  204.  
  205. // Log this error to the global array..
  206.  
  207. $EZSQL_ERROR[] = array
  208.  
  209. (
  210.  
  211. "query" => $this->last_query,
  212.  
  213. "error_str" => $str,
  214.  
  215. "error_no" => $error_no
  216.  
  217. );
  218.  
  219.  
  220.  
  221. // Is error output turned on or not..
  222.  
  223. if ( $this->show_errors )
  224.  
  225. {
  226.  
  227. // If there is an error then take note of it
  228.  
  229. print "<blockquote><font face=arial size=2 color=ff0000>";
  230.  
  231. print "<b>SQL/DB Error --</b> ";
  232.  
  233. print "[<font color=000077>$str</font>]";
  234.  
  235. print "</font></blockquote>";
  236.  
  237. }
  238.  
  239. else
  240.  
  241. {
  242.  
  243. return false;
  244.  
  245. }
  246.  
  247. }
  248.  
  249.  
  250.  
  251. // ==================================================================
  252.  
  253. // Turn error handling on or off..
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. function show_errors()
  261.  
  262. {
  263.  
  264. $this->show_errors = true;
  265.  
  266. }
  267.  
  268.  
  269. function hide_errors()
  270.  
  271. {
  272.  
  273. $this->show_errors = false;
  274.  
  275. }
  276.  
  277.  
  278.  
  279. // ==================================================================
  280.  
  281. // Kill cached query results
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288. function flush()
  289.  
  290. {
  291.  
  292.  
  293.  
  294. // Get rid of these
  295.  
  296. $this->last_result = null;
  297.  
  298. $this->col_info = null;
  299.  
  300. $this->last_query = null;
  301.  
  302.  
  303.  
  304. }
  305.  
  306.  
  307.  
  308. // ==================================================================
  309.  
  310. // Basic Query - see docs for more detail
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317. function query($query)
  318.  
  319. {
  320.  
  321.  
  322. // For reg expressions
  323.  
  324. $query = trim($query);
  325.  
  326.  
  327. // initialise return
  328.  
  329. $return_val = 0;
  330.  
  331.  
  332.  
  333. // Flush cached values..
  334.  
  335. $this->flush();
  336.  
  337.  
  338.  
  339. // Log how the function was called
  340.  
  341. $this->func_call = "\$db->query(\"$query\")";
  342.  
  343.  
  344.  
  345. // Keep track of the last query for debug..
  346.  
  347. $this->last_query = $query;
  348.  
  349.  
  350.  
  351. // Perform the query via std mysql_query function..
  352.  
  353. $this->result = @mysql_query($query,$this->dbh);
  354.  
  355. $this->num_queries++;
  356.  
  357.  
  358.  
  359. // If there is an error then take note of it..
  360.  
  361. if ( mysql_error() )
  362.  
  363. {
  364.  
  365. $this->print_error();
  366.  
  367. return false;
  368.  
  369. }
  370.  
  371.  
  372. // Query was an insert, delete, update, replace
  373.  
  374. if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
  375.  
  376. {
  377.  
  378. $this->rows_affected = mysql_affected_rows();
  379.  
  380.  
  381. // Take note of the insert_id
  382.  
  383. if ( preg_match("/^(insert|replace)\s+/i",$query) )
  384.  
  385. {
  386.  
  387. $this->insert_id = mysql_insert_id($this->dbh);
  388.  
  389. }
  390.  
  391.  
  392. // Return number fo rows affected
  393.  
  394. $return_val = $this->rows_affected;
  395.  
  396. }
  397.  
  398. // Query was an select
  399.  
  400. else
  401.  
  402. {
  403.  
  404.  
  405. // Take note of column info
  406.  
  407. $i=0;
  408.  
  409. while ($i < @mysql_num_fields($this->result))
  410.  
  411. {
  412.  
  413. $this->col_info[$i] = @mysql_fetch_field($this->result);
  414.  
  415. $i++;
  416.  
  417. }
  418.  
  419.  
  420. // Store Query Results
  421.  
  422. $num_rows=0;
  423.  
  424. while ( $row = @mysql_fetch_object($this->result) )
  425.  
  426. {
  427.  
  428. // Store relults as an objects within main array
  429.  
  430. $this->last_result[$num_rows] = $row;
  431.  
  432. $num_rows++;
  433.  
  434. }
  435.  
  436.  
  437.  
  438. @mysql_free_result($this->result);
  439.  
  440.  
  441.  
  442. // Log number of rows the query returned
  443.  
  444. $this->num_rows = $num_rows;
  445.  
  446.  
  447. // Return number of rows selected
  448.  
  449. $return_val = $this->num_rows;
  450.  
  451. }
  452.  
  453.  
  454.  
  455. // If debug ALL queries
  456.  
  457. $this->trace || $this->debug_all ? $this->debug() : null ;
  458.  
  459.  
  460.  
  461. return $return_val;
  462.  
  463.  
  464.  
  465. }
  466.  
  467.  
  468.  
  469. // ==================================================================
  470.  
  471. // Get one variable from the DB - see docs for more detail
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478. function get_var($query=null,$x=0,$y=0)
  479.  
  480. {
  481.  
  482. $GLOBALS['query_log'][] = $query;
  483.  
  484. // Log how the function was called
  485.  
  486. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  487.  
  488.  
  489.  
  490. // If there is a query then perform it if not then use cached results..
  491.  
  492. if ( $query )
  493.  
  494. {
  495.  
  496. $this->query($query);
  497.  
  498. }
  499.  
  500.  
  501.  
  502. // Extract var out of cached results based x,y vals
  503.  
  504. if ( $this->last_result[$y] )
  505.  
  506. {
  507.  
  508. $values = array_values(get_object_vars($this->last_result[$y]));
  509.  
  510. }
  511.  
  512.  
  513.  
  514. // If there is a value return it else return null
  515.  
  516. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  517.  
  518. }
  519.  
  520.  
  521.  
  522. // ==================================================================
  523.  
  524. // Get one row from the DB - see docs for more detail
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531. function get_row($query=null,$output=OBJECT,$y=0)
  532.  
  533. {
  534.  
  535.  
  536.  
  537. // Log how the function was called
  538.  
  539. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  540.  
  541.  
  542.  
  543. // If there is a query then perform it if not then use cached results..
  544.  
  545. if ( $query )
  546.  
  547. {
  548.  
  549. $this->query($query);
  550.  
  551. }
  552.  
  553.  
  554.  
  555. // If the output is an object then return object using the row offset..
  556.  
  557. if ( $output == OBJECT )
  558.  
  559. {
  560.  
  561. return $this->last_result[$y]?$this->last_result[$y]:null;
  562.  
  563. }
  564.  
  565. // If the output is an associative array then return row as such..
  566.  
  567. elseif ( $output == ARRAY_A )
  568.  
  569. {
  570.  
  571. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  572.  
  573. }
  574.  
  575. // If the output is an numerical array then return row as such..
  576.  
  577. elseif ( $output == ARRAY_N )
  578.  
  579. {
  580.  
  581. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  582.  
  583. }
  584.  
  585. // If invalid output type was specified..
  586.  
  587. else
  588.  
  589. {
  590.  
  591. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  592.  
  593. }
  594.  
  595.  
  596.  
  597. }
  598.  
  599.  
  600.  
  601. // ==================================================================
  602.  
  603. // Function to get 1 column from the cached result set based in X index
  604.  
  605. // se docs for usage and info
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612. function get_col($query=null,$x=0)
  613.  
  614. {
  615.  
  616.  
  617.  
  618. // If there is a query then perform it if not then use cached results..
  619.  
  620. if ( $query )
  621.  
  622. {
  623.  
  624. $this->query($query);
  625.  
  626. }
  627.  
  628.  
  629.  
  630. // Extract the column values
  631.  
  632. for ( $i=0; $i < count($this->last_result); $i++ )
  633.  
  634. {
  635.  
  636. $new_array[$i] = $this->get_var(null,$x,$i);
  637.  
  638. }
  639.  
  640.  
  641.  
  642. return $new_array;
  643.  
  644. }
  645.  
  646.  
  647.  
  648. // ==================================================================
  649.  
  650. // Return the the query as a result set - see docs for more details
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657. function get_results($query=null, $output = OBJECT)
  658.  
  659. {
  660.  
  661. $GLOBALS['query_log'][] = $query;
  662.  
  663. // Log how the function was called
  664.  
  665. $this->func_call = "\$db->get_results(\"$query\", $output)";
  666.  
  667.  
  668.  
  669. // If there is a query then perform it if not then use cached results..
  670.  
  671. if ( $query )
  672.  
  673. {
  674.  
  675. $this->query($query);
  676.  
  677. }
  678.  
  679.  
  680.  
  681. // Send back array of objects. Each row is an object
  682.  
  683. if ( $output == OBJECT )
  684.  
  685. {
  686.  
  687. return $this->last_result;
  688.  
  689. }
  690.  
  691. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  692.  
  693. {
  694.  
  695. if ( $this->last_result )
  696.  
  697. {
  698.  
  699. $i=0;
  700.  
  701. foreach( $this->last_result as $row )
  702.  
  703. {
  704.  
  705.  
  706.  
  707. $new_array[$i] = get_object_vars($row);
  708.  
  709.  
  710.  
  711. if ( $output == ARRAY_N )
  712.  
  713. {
  714.  
  715. $new_array[$i] = array_values($new_array[$i]);
  716.  
  717. }
  718.  
  719.  
  720.  
  721. $i++;
  722.  
  723. }
  724.  
  725.  
  726.  
  727. return $new_array;
  728.  
  729. }
  730.  
  731. else
  732.  
  733. {
  734.  
  735. return null;
  736.  
  737. }
  738.  
  739. }
  740.  
  741. }
  742.  
  743.  
  744.  
  745.  
  746.  
  747. // ==================================================================
  748.  
  749. // Function to get column meta data info pertaining to the last query
  750.  
  751. // see docs for more info and usage
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758. function get_col_info($info_type="name",$col_offset=-1)
  759.  
  760. {
  761.  
  762.  
  763.  
  764. if ( $this->col_info )
  765.  
  766. {
  767.  
  768. if ( $col_offset == -1 )
  769.  
  770. {
  771.  
  772. $i=0;
  773.  
  774. foreach($this->col_info as $col )
  775.  
  776. {
  777.  
  778. $new_array[$i] = $col->{$info_type};
  779.  
  780. $i++;
  781.  
  782. }
  783.  
  784. return $new_array;
  785.  
  786. }
  787.  
  788. else
  789.  
  790. {
  791.  
  792. return $this->col_info[$col_offset]->{$info_type};
  793.  
  794. }
  795.  
  796.  
  797.  
  798. }
  799.  
  800.  
  801.  
  802. }
  803.  
  804.  
  805.  
  806.  
  807.  
  808. // ==================================================================
  809. // Dumps the contents of any input variable to screen in a nicely
  810.  
  811. // formatted and easy to understand way - any type: Object, Var or Array
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818. function vardump($mixed='')
  819.  
  820. {
  821.  
  822.  
  823.  
  824. echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  825.  
  826. echo "<pre><font face=arial>";
  827.  
  828.  
  829.  
  830. if ( ! $this->vardump_called )
  831.  
  832. {
  833.  
  834. echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  835.  
  836. }
  837.  
  838.  
  839.  
  840. $var_type = gettype ($mixed);
  841.  
  842. print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
  843.  
  844. echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  845.  
  846. echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  847.  
  848. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  849.  
  850. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  851.  
  852. echo "</font></pre></font></blockquote></td></tr></table>".$this->donation();
  853.  
  854. echo "\n<hr size=1 noshade color=dddddd>";
  855.  
  856.  
  857.  
  858. $this->vardump_called = true;
  859.  
  860.  
  861.  
  862. }
  863.  
  864.  
  865.  
  866. // Alias for the above function
  867.  
  868. function dumpvar($mixed)
  869.  
  870. {
  871.  
  872. $this->vardump($mixed);
  873.  
  874. }
  875.  
  876.  
  877.  
  878. // ==================================================================
  879. // Displays the last query string that was sent to the database & a
  880.  
  881. // table listing results (if there were any).
  882.  
  883. // (abstracted into a seperate file to save server overhead).
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890. function debug()
  891.  
  892. {
  893.  
  894.  
  895.  
  896. echo "<blockquote>";
  897.  
  898.  
  899.  
  900. // Only show ezSQL credits once..
  901.  
  902. if ( ! $this->debug_called )
  903.  
  904. {
  905.  
  906. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  907.  
  908. }
  909.  
  910. echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  911.  
  912. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  913.  
  914.  
  915.  
  916. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  917.  
  918. echo "<blockquote>";
  919.  
  920.  
  921.  
  922. if ( $this->col_info )
  923.  
  924. {
  925.  
  926.  
  927.  
  928. // =====================================================
  929.  
  930. // Results top rows
  931.  
  932.  
  933.  
  934. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  935.  
  936. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  937.  
  938.  
  939.  
  940.  
  941.  
  942. for ( $i=0; $i < count($this->col_info); $i++ )
  943.  
  944. {
  945.  
  946. echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>{$this->col_info[$i]->name}</span></td>";
  947.  
  948. }
  949.  
  950.  
  951.  
  952. echo "</tr>";
  953.  
  954.  
  955.  
  956. // ======================================================
  957.  
  958. // print main results
  959.  
  960.  
  961.  
  962. if ( $this->last_result )
  963.  
  964. {
  965.  
  966.  
  967.  
  968. $i=0;
  969.  
  970. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  971.  
  972. {
  973.  
  974. $i++;
  975.  
  976. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  977.  
  978.  
  979.  
  980. foreach ( $one_row as $item )
  981.  
  982. {
  983.  
  984. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  985.  
  986. }
  987.  
  988.  
  989.  
  990. echo "</tr>";
  991.  
  992. }
  993.  
  994.  
  995.  
  996. } // if last result
  997.  
  998. else
  999.  
  1000. {
  1001.  
  1002. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  1003.  
  1004. }
  1005.  
  1006.  
  1007.  
  1008. echo "</table>";
  1009.  
  1010.  
  1011.  
  1012. } // if col_info
  1013.  
  1014. else
  1015.  
  1016. {
  1017.  
  1018. echo "<font face=arial size=2>No Results</font>";
  1019.  
  1020. }
  1021.  
  1022.  
  1023.  
  1024. echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030. $this->debug_called = true;
  1031.  
  1032. }
  1033.  
  1034.  
  1035.  
  1036. // =======================================================
  1037. // Naughty little function to ask for some remuniration!
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044. function donation()
  1045.  
  1046. {
  1047.  
  1048. return "<font size=1 face=arial color=000000>If ezSQL has helped <a href=\"https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0\" style=\"color: 0000CC;\">make a donation!?</a> &nbsp;&nbsp;[ go on! you know you want to! ]</font>";
  1049.  
  1050. }
  1051.  
  1052.  
  1053.  
  1054. }
  1055.  
  1056.  
  1057.  
  1058. $db = new db(EZSQL_DB_USER, EZSQL_DB_PASSWORD, EZSQL_DB_NAME, EZSQL_DB_HOST);
  1059.  
  1060.  
  1061.  
  1062. ?>

Documentation generated on Tue, 24 May 2005 03:57:39 -0400 by phpDocumentor 1.3.0RC3